
$(document).ready(function(){
    var hrefStr = window.location.href;
    var arr1=hrefStr.split("?");
    var tip = arr1[1].split("=")[1];
    console.log(tip);
    var qes = {
        url:"../json/detail_goodsInfor.json",
        success:function(res){
            // console.log(res);
            // console.log(res.goodsses[tip]);
            var loadData = res.goodsses[tip];
            //当前路径
            $("#cont>.headline").html("");
            // console.log(loadData);
            loadData.url.forEach(function(ele,index){
                var oA = document.createElement("a");
                oA.href = "#";
                oA.innerText = ele;
                var oSpan = document.createElement("span");
                oSpan.innerText=">";
                $("#cont>.headline").append(oA).append(oSpan);

            })
            //左侧栏添加小图片
            loadData.imgSrc_s.forEach(function(ele, index){
               var picLi = document.createElement("li");
               $(picLi).appendTo($(".smallpic>ul"));
               $(picLi).html("<img src='"+ele+"'/>")
            })
            $(".smallpic>ul>li").on("click",function(){
                var self = this;
                $(loadData.imgSrc_s).each(function(index,ele){
                    var elesrc = ele.split("..")[1];
                    var selfsrc= $(self).children("img")[0].src.split("src")[1];
                    if(elesrc==selfsrc){
                        console.log(loadData.imgSrc_b[index]);

                        $(".midpic").children("img")[0].src = loadData.imgSrc_b[index];
                        $(".bigpic").children("img")[0].src = loadData.imgSrc_b[index];
                    }
                })
                // $(".midpic>img").prop("src",$(this).find("img")[0].src+"o");
            })
            //小图移动效果(当图片张数大于4的时候有效)
            if($(".smallpic>ul>li").length>4){
                $(".smallpic>em").on("click",function(){
                    $(".smallpic>ul").prepend($(".smallpic>ul>li:last").clone(true));
                    $(".smallpic>ul>li").css("top",-89);
                    var flag = 0;
                    $(".smallpic>ul>li").stop().animate({"top":0},800,"swing",function(){
                        flag++;
                        if(flag >=$(".smallpic>ul>li").length){

                            $(".smallpic>ul").prepend($(".smallpic>ul>li:last"));
                            $(".smallpic>ul>li:first").remove();
                        }
                    });
                })
                $(".smallpic>i").on("click",function(){
                    var flag = 0;
                    $(".smallpic>ul>li").stop().animate({"top":-89},800,"swing",function(){
                        flag++;
                        if(flag >=$(".smallpic>ul>li").length){
                            $(".smallpic>ul>li").css("top",0);
                            $(".smallpic>ul").append($(".smallpic>ul>li:first"));
                        }
                    });
                })

            }

            //放大镜效果的图片的src改变
            $(".midpic>img").prop("src",loadData.imgSrc_b[0]);
            $(".bigpic>img").prop("src",loadData.imgSrc_b[0]);
            //商品信息的商品名改变
            $(".goodsInfor>h2").text(loadData.name);
            //商品编号
            $("#goodsnum").text(loadData.number);
            //商品价格
            $("#pricetag").text(loadData.price);
            //打折商品或者有商品详情的导入其商品详情
            if(loadData.sale){
                $(".flex>.tip2>p").text(loadData.sale);
            }else{
                $(".flex>.tip").remove();
                $(".describe>.open").remove();
            }
            //加入购物车请求
            $("#addcar").on("click",function(){
                //判断cookie中有无用户登录
                //有则存入数据库，没有就存入cookie临时放存，等用户登录时转入数据库中
                if($.cookie("spInfor")){
                    var currentUser = JSON.parse($.cookie("spInfor")).username;
                    //检测到当前登录用户
                    console.log(currentUser);
                    console.log("有用户登录，存入数据库");
                    var goodsnum = loadData.number;
                    console.log(tip);
                    console.log($("#numsp").val());
                    var goodsdata = {
                        "url":"../hz_1706/spUser/version1/spcar.php",
                        "type":"post",
                        "data":{serial:tip,sum:$("#numsp").val(),phonenumber:currentUser},
                        "dataType":"json",
                        "success":function(res){
                            console.log(res);
                        }
                    }
                    $.ajax(goodsdata);
                }
                else{
                    //未检测到当前用户
                    console.log("没有用户登录，存入cookie");
                    var flag = 0;
                    if($.cookie("goodsses")){
                        var prevData = JSON.parse($.cookie("goodsses"));
                        //prevData为一个数组[{serial:307917,sum:2},{serial:145831,num:1}..]
                        prevData.forEach(function(ele,index,arr){
                            var coe = true;
                            //console.log(ele.serial, loadData.number);
                            if(ele.serial==tip){
                                ele.sum++;
                               coe= false;

                            }
                            //console.log(coe);
                            if(coe){
                                flag++;
                            }
                        })

                        if(flag==prevData.length){
                            prevData.push({serial:tip,sum:$("#numsp").val()})
                        }
                        $.cookie("goodsses",JSON.stringify(prevData));
                        console.log(JSON.parse($.cookie("goodsses")));

                    }
                    else{
                        //购物车还是空的，直接写入
                        $.cookie("goodsses",JSON.stringify([{serial:tip,sum:$("#numsp").val()}]));
                        console.log(JSON.parse($.cookie("goodsses")));

                    }




                }
                
                
            })



        }
    }
    $.ajax(qes);

    $(".midpic").on("mousemove",function(evt){
        var e = evt||window.event;
        $("#shadow").css({display:"block"});
        $(".bigpic").css({display:"block"})
        // console.log(e.clientY);
        // console.log($(".midpic")[0].offsetTop);
        $(".midpic>#shadow").css({left:e.clientX-$(".midpic")[0].offsetLeft-90,
            top:e.clientY-$(".midpic")[0].offsetTop-90+$(window).scrollTop()});
        // console.log($("#shadow")[0].offsetLeft);
        if($("#shadow")[0].offsetLeft<=0){
            $("#shadow").css({left:0})
        }
        if($("#shadow")[0].offsetLeft>=190){
            $("#shadow").css({left:190})
        }
        if($("#shadow")[0].offsetTop<=0){
            $("#shadow").css({top:0})
        }
        if($("#shadow")[0].offsetTop>=190){
            $("#shadow").css({top:190})
        }
        // $(".midpic>img").css({left:})
        $(".bigpic>img").css({left:-$("#shadow")[0].offsetLeft*2,top:-$("#shadow")[0].offsetTop*2});


    });

    $(".midpic").on("mouseleave",function(evt){
        $("#shadow").css({display:"none"});
        $(".bigpic").css({display:"none"});
    })


})
